package org.mat.ipaas.ucenter.web.single.modules.oss.controller;

import javax.servlet.http.HttpServletRequest;

import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.biz.common.system.query.QueryGenerator;
import org.mat.ipaas.ucenter.biz.modules.oss.entity.OssFile;
import org.mat.ipaas.ucenter.biz.modules.oss.service.IOssFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;

/**
 * 云存储示例 DEMO
 */
@Slf4j
@Controller
@RequestMapping("/sys/oss/file")
public class OssFileController {

    @Autowired
    private IOssFileService ossFileService;

    @ResponseBody
    @GetMapping("/list")
    public MatApiResponse<IPage<OssFile>> queryPageList(OssFile file,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        MatApiResponse<IPage<OssFile>> matApiResponse = new MatApiResponse<>();
        QueryWrapper<OssFile> queryWrapper = QueryGenerator.initQueryWrapper(file, req.getParameterMap());
        Page<OssFile> page = new Page<>(pageNo, pageSize);
        IPage<OssFile> pageList = ossFileService.page(page, queryWrapper);
        matApiResponse.setSuccess(true);
        matApiResponse.setData(pageList);
        return matApiResponse;
    }

    @ResponseBody
    @PostMapping("/upload")
    //@RequiresRoles("admin")
    //@RequiresPermissions("system:ossFile:upload")
    public MatApiResponse upload(@RequestParam("file") MultipartFile multipartFile) {
        MatApiResponse matApiResponse = new MatApiResponse();
        try {
            ossFileService.upload(multipartFile);
            matApiResponse.success("上传成功！");
        } catch (Exception ex) {
            log.info(ex.getMessage(), ex);
            matApiResponse.fail("上传失败");
        }
        return matApiResponse;
    }

    @ResponseBody
    @DeleteMapping("/delete")
    public MatApiResponse delete(@RequestParam(name = "id") String id) {
        MatApiResponse matApiResponse = new MatApiResponse();
        OssFile file = ossFileService.getById(id);
        if (file == null) {
            matApiResponse.fail("未找到对应实体");
        } else {
            boolean ok = ossFileService.delete(file);
            matApiResponse.success("删除成功!");
        }
        return matApiResponse;
    }

    /**
     * 通过id查询.
     */
    @ResponseBody
    @GetMapping("/queryById")
    public MatApiResponse<OssFile> queryById(@RequestParam(name = "id") String id) {
        MatApiResponse<OssFile> matApiResponse = new MatApiResponse<>();
        OssFile file = ossFileService.getById(id);
        if (file == null) {
            matApiResponse.fail("未找到对应实体");
        } else {
            matApiResponse.setData(file);
            matApiResponse.setSuccess(true);
        }
        return matApiResponse;
    }

}
